//
//  UserGuideCircleNoteView.m
//  etionUI
//
//  Created by WangJian on 14-2-24.
//  Copyright (c) 2014年 GuangZhouXuanWu. All rights reserved.
//

#import "UserGuideCircleNoteView.h"

@implementation CUserGuideCircleNoteView
{
    CGPoint arrowBodyStartPoint;
    CGPoint arrowBodyEndPoint;
    CGPoint arrowBodyControlPoint1;
    CGPoint arrowBodyControlPoint2;
    
    CGPoint arrowHeadStartPoint;
    CGPoint arrowHeadEndPoint;
    
    UILabel *infoLable;
    
    BOOL isMidArrow;
}

- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        // Initialization code
        
    }
    return self;
}

- (void)didMoveToSuperview
{
    if (self.superview != nil)
    {
        [self calculateArrowPosition];
        [self drawInfo];
    }
}

- (void)calculateArrowPosition
{
    if (CGRectGetMaxX(self.targetFrame) < self.superview.width/2)
    {
        arrowBodyEndPoint.x = CGRectGetMaxX(self.targetFrame);
    }
    else if (CGRectGetMinX(self.targetFrame) > self.superview.width/2)
    {
        arrowBodyEndPoint.x = CGRectGetMinX(self.targetFrame);
    }
    else
    {
        arrowBodyEndPoint.x = CGRectGetMidX(self.targetFrame)+40;
        isMidArrow = YES;
    }
    
    if (CGRectGetMaxY(self.targetFrame) < self.superview.height/2)
    {
        arrowBodyEndPoint.y = CGRectGetMaxY(self.targetFrame) + (isMidArrow ? 5 : 0);
    }
    else
    {
        arrowBodyEndPoint.y = CGRectGetMinY(self.targetFrame) - (isMidArrow ? 5 : 0);
    }
    
    arrowBodyStartPoint.x = arrowBodyEndPoint.x > CGRectGetMinX(self.targetFrame) ? arrowBodyEndPoint.x + 20 : arrowBodyEndPoint.x - 20;
    arrowBodyStartPoint.y = arrowBodyEndPoint.y > CGRectGetMinY(self.targetFrame) ? arrowBodyEndPoint.y + 40 : arrowBodyEndPoint.y - 40;

    const NSInteger controlOffset = 10;
    CGPoint midPoint = CGPointMake((arrowBodyStartPoint.x+arrowBodyEndPoint.x)/2, (arrowBodyStartPoint.y+arrowBodyEndPoint.y)/2);
    
    arrowBodyControlPoint1.x = arrowBodyStartPoint.x>arrowBodyEndPoint.x ? midPoint.x+controlOffset/2 : midPoint.x-controlOffset/2;
    arrowBodyControlPoint1.y = arrowBodyStartPoint.y>arrowBodyEndPoint.y ? midPoint.y-controlOffset : midPoint.y+controlOffset;
    
    arrowBodyControlPoint2.x = arrowBodyStartPoint.x>arrowBodyEndPoint.x ? midPoint.x+controlOffset : midPoint.x-controlOffset;
    arrowBodyControlPoint2.y = arrowBodyStartPoint.y>arrowBodyEndPoint.y ? midPoint.y-controlOffset/2 : midPoint.y+controlOffset/2;
    
    const NSInteger headSize = 10;
    arrowHeadStartPoint.x = arrowBodyStartPoint.x>arrowBodyEndPoint.x ? arrowBodyEndPoint.x + headSize/5 : arrowBodyEndPoint.x - headSize/5;
    arrowHeadStartPoint.y = arrowBodyStartPoint.y>arrowBodyEndPoint.y ? arrowBodyEndPoint.y + headSize : arrowBodyEndPoint.y - headSize;
    
    arrowHeadEndPoint.x = arrowBodyStartPoint.x>arrowBodyEndPoint.x ? arrowBodyEndPoint.x + headSize : arrowBodyEndPoint.x - headSize;
    arrowHeadEndPoint.y = arrowBodyStartPoint.y>arrowBodyEndPoint.y ? arrowBodyEndPoint.y - headSize*1/10 : arrowBodyEndPoint.y + headSize*1/10;
}

- (void)drawInfo
{
    UIBezierPath *bezierPath = [UIBezierPath bezierPathWithOvalInRect:self.targetFrame];
    
    [bezierPath moveToPoint:arrowBodyEndPoint];
    [bezierPath addCurveToPoint:arrowBodyStartPoint controlPoint1:arrowBodyControlPoint1 controlPoint2:arrowBodyControlPoint2];
    
    [bezierPath moveToPoint:arrowHeadStartPoint];
    [bezierPath addLineToPoint:arrowBodyEndPoint];
    [bezierPath addLineToPoint:arrowHeadEndPoint];
    
    CAShapeLayer *testLayer = [CAShapeLayer layer];
    testLayer.frame = self.bounds;
    testLayer.path = [bezierPath CGPath];
    testLayer.fillColor = [[UIColor clearColor] CGColor];
    testLayer.strokeColor = [[UIColor whiteColor] CGColor];
    testLayer.lineWidth = 4;
    testLayer.shadowColor = [UIColor blackColor].CGColor;
    testLayer.shadowOpacity = .3;
    testLayer.shadowOffset = CGSizeMake(.0, .0);
    testLayer.shadowRadius = .5;
    testLayer.lineCap = @"round";
    [self.superview.layer addSublayer:testLayer];
    
    CABasicAnimation *pathAnimation = [CABasicAnimation animationWithKeyPath:@"strokeEnd"];
    pathAnimation.duration = 1.0;
    pathAnimation.fromValue = [NSNumber numberWithFloat:0.0f];
    pathAnimation.toValue = [NSNumber numberWithFloat:1.0f];
    [testLayer addAnimation:pathAnimation forKey:@"strokeEndAnimation"];
    
    if (self.information.length > 0)
    {
        if (infoLable == nil)
        {
            infoLable = [[UILabel alloc] initWithFrame:CGRectZero];
            infoLable.font = [UIFont boldSystemFontOfSize:14];
            infoLable.textColor = [UIColor whiteColor];
            infoLable.backgroundColor = [UIColor clearColor];
            infoLable.textAlignment = NSTextAlignmentCenter;
            infoLable.numberOfLines = 0;
            [self.superview addSubview:infoLable];
            [infoLable release];
        }
      
        CGSize textSize = [self.information stringSizeWithFont:infoLable.font forConstrainSize:CGSizeMake(self.superview.width/(isMidArrow ? 2 : 3), 100)];
        infoLable.text = self.information;
        CGPoint textStartPoint = CGPointMake(arrowBodyStartPoint.x-textSize.width/2, arrowBodyStartPoint.y);
        if (arrowBodyStartPoint.y < arrowBodyEndPoint.y)
        {
            textStartPoint.y -= textSize.height+5;
        }
        else
        {
            textStartPoint.y += 5;
        }
        
        if (textStartPoint.x < 20)
        {
            textStartPoint.x = 20;
        }
        
        infoLable.alpha = 0.0;
        infoLable.frame = CGRectMake(textStartPoint.x, textStartPoint.y + (arrowBodyStartPoint.y < arrowBodyEndPoint.y ? 1 : -1) * 20, textSize.width, textSize.height);
        [UIView animateWithDuration:.5 delay:1.0 options:UIViewAnimationCurveEaseOut animations:^{
            infoLable.alpha = 1.0;
            infoLable.frame = CGRectMake(textStartPoint.x, textStartPoint.y, textSize.width, textSize.height);
        } completion:^(BOOL finished) {
            
        }];
    }
}

@end
